package com.rupertjones.globalcron.web;

import com.rupertjones.globalcron.domain.Role;
import com.rupertjones.globalcron.domain.Setting;

import java.util.ArrayList;
import java.util.Date;
import java.util.Set;

import static org.apache.commons.lang.StringUtils.isEmpty;

/**
 * <p>&copy Rupert Jones 2011,2012</p>
 *
 * @author rup
 */
public class FormUser {

    private String password;
    private long id;
    private String confirmPassword;
    private Setting passwordPolicy;
    private String fullName;
    private String username;
    private Date lastLogin;
    private ArrayList<FormRole> roles = new ArrayList<FormRole>();

    public FormUser() {

    }

    public FormUser(Setting setting) {
        this.passwordPolicy = setting;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public Date getLastLogin() {
        return lastLogin;
    }

    public void setLastLogin(Date lastLogin) {
        this.lastLogin = lastLogin;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public ArrayList<FormRole> getRoles() {
        return roles;
    }

    public void setRoles(ArrayList<FormRole> roles) {
        this.roles = roles;
    }

    public void addRole(FormRole role) {
        this.roles.add(role);
    }

    public boolean isPasswordPolicyCompliant() {
        return !isEmpty(password) && password.matches(passwordPolicy.getValue());
    }

    public boolean isPasswordMatch() {
        return !isEmpty(password) && password.equals(confirmPassword);
    }

    public boolean hasPassword() {
        return !isEmpty(password);
    }

    public void clearRoles() {
        this.roles.clear();
    }

    public String getPrintRoles() {
        StringBuilder builder = new StringBuilder();

        for (FormRole role : roles) {
            builder.append(role.getName());
            builder.append(" ");
        }
        return builder.toString().trim().replace(" ", ",");
    }

    public FormRole getRole(String value) {
        for (FormRole formRole : roles) {
            if (formRole.getName().equals(value)) {
                return formRole;
            }
        }
        return null;
    }

    public void unassignRoles() {
        for (FormRole formRole : roles) {
            formRole.setAssigned(false);
        }
    }

    public void setRoles(Set<Role> inputRoles) {
        for (Role role : inputRoles) {
            FormRole thisRole = new FormRole();
            thisRole.setAssigned(true);
            thisRole.setName(role.getName());
            roles.add(thisRole);
        }
    }
}
